/**
 * 根据ID删除
 */
function deleteConfirm(moduleName, uuid, isPhysically) {
	if (!uuid || !moduleName) {
		return;
	}
	var para = {
		"uuid" : uuid
	};
	var method = null;
	var tip = "";
	if (isPhysically) {
		method = "/delete";
		tip = "删除";
	} else {
		method = "/logoff";
		tip = "注销";
	}
	layer.confirm("确定" + tip + "？", {
		icon : 3,
		title : tip + '确认'
	}, function(index) {
		layer.close(index);
		$.post(webRoot + "/" + moduleName + method, para, function(out) {
			if (out && out.code == 500) {
				layer.msg(out.msg, {
					icon : 5
				});
				return;
			} else {
				layer.msg(tip + "成功！", {
					icon : 6
				});
			}
			$('#' + moduleName + 'Table').bootstrapTable("removeByUniqueId", uuid);
		}).error(function() {
			layer.msg("请求出错！", {
				icon : 7
			});
		});
	});
}

/**
 * 重置弹出框高度
 */
function resetDlgHeight() {
	if ($(".dlg").length == 0) {
		return;
	}
	var availableHeight = $(window).height();// 可视窗口高度
	var dlgHeight = $(".dlg").height();
	var top = $(".dlg").position().top;
	var bottom = availableHeight - (top + dlgHeight);
	if (dlgHeight + 50 > availableHeight) {
		if ($(".layui-layer-btn").length != 0) {
			$(".layui-layer-content").height(availableHeight - 100);// 有按钮：超出最大高度，高度重置
		} else {
			$(".layui-layer-content").height(availableHeight - 50);// 无按钮：超出最大高度，高度重置
		}
		$(".dlg").css("top", 25);// 边距重置
	} else if (bottom < 0 || bottom != top) {
		var targetDlgHeight = (availableHeight - dlgHeight) / 2 + (availableHeight - dlgHeight) % 2;
		$(".dlg").css("top", targetDlgHeight);// 边距重置
	}
}

/**
 * @param url
 *            请求url
 * @param width
 *            对话框宽度
 * @param params
 *            参数:json格式
 * @param btnType
 *            按钮种类
 */
function showDlg(url, width, params, btnType) {
	layer.closeAll();
	if (url.lastIndexOf("/") == url.length - 1) {
		url = url.substring(0, url.lastIndexOf("/"));
	}
	layerIndex = layer.load(1, {
		shade : [ 0.5, '#fff' ]
	});// 0.1透明度的白色背景
	if (!width) {
		width = "600px";
	}
	var dlgBtn = null;
	if (btnType) {
		dlgBtn = {
			btn : [ '确定', '取消' ],
			yes : function() {
				return false;
			},
			cancel : function() {
				return false;
			}
		};
	} else {
		dlgBtn = {
			btn : '关闭',
			yes : function() {
				layer.closeAll();
			}
		};
	}
	$.post(url, params, function(out) {
		layer.close(layerIndex);
		var options = {
			type : 1,// 基本层类型
			title : false,// 类型：String/Array/Boolean，默认：'信息'
			closeBtn : true,// 是否有关闭按钮
			shadeClose : false,// 是否点击遮罩关闭
			area : width,// ['420px', '240px'], 宽高
			skin : 'dlg',// 自定义样式
			// offset : '25px',// ['500px', '300px'],top,left
			content : out
		// 弹窗内容
		};
		$.extend(options, dlgBtn);
		if (out && out.code == 500) {
			layer.msg(out.msg, {
				icon : 5
			});
			return;
		}
		layerIndex = layer.open(options);
		$(window).keydown(function(event) {
			if (event.keyCode == 27) {
				layer.close(layerIndex);
			}
		});
		setTimeout(function() {
			resetDlgHeight();
		}, 1000);
		setTimeout(function() {
			resetDlgHeight();
		}, 3000);
	}).error(function() {
		layer.closeAll();
		layer.msg("请求出错！", {
			icon : 7
		});
	});
}

/**
 * /** 适用范围：搜索对话框ID为:模块名+Condition,列表DIV的ID为:模块名+TableContainer
 * 列表action为:tableList
 * 
 * @param module
 */
function refreshTable(module, params) {
	// if (params) {
	// $('#' + module + 'Table').bootstrapTable("updateRow", params);
	// return;
	// }
	$('#' + module + 'Table').bootstrapTable('refresh', {
		'silent' : true
	});
};

/**
 * @param url
 *            请求URL
 * @param formId
 *            表单ID
 * @param urlOut
 *            请求成功后跳转的URL
 * @param afterSucc
 *            回调
 */
function submitForm(url, formId, urlOut, afterSucc) {
	if ($(formId).serializeArray().length == 0) {
		layer.msg("表单内没有数据", {
			icon : 5
		});
		return;
	}
	$.post(url, $(formId).serializeArray(), function(out) {
		if (out.jfinal_token) {
			$("input[name='jfinal_token']").val(out.jfinal_token);
		}
		if (out && out.code == 500) {
			layer.msg(out.msg, {
				icon : 5
			});
			return;
		}
		if (out.validateFailed != null) {
			layer.msg(out.validateFailed, {
				icon : 5
			});
			return;
		}
		if (typeof (afterSucc) == 'function') {
			afterSucc(out);
		}
		if (urlOut) {
			location.href = urlOut;
		}
	}).error(function() {
		layer.close(index);
		layer.msg("请求出错！", {
			icon : 7
		});
	});
}

function loginOut() {
	layer.confirm("确定登出？", {
		icon : 3
	}, function(index) {
		location.href = webRoot + "/background/loginOut";
	});
}

/**
 * 改变某状态
 * 
 * @param moduleName
 *            模块
 * @param method
 *            action
 * @param uuid
 * @param state
 *            状态值
 * @param index
 *            行号
 * @param tips
 *            提示
 */
function changeState(moduleName, method, uuid, state, index, tips, div) {
	if (!uuid || !moduleName || !method || index == null) {
		layer.msg("出错啦！");
		return;
	}
	var para = {
		"uuid" : uuid,
		"index" : index
	};
	if (state) {
		$.extend(para, {
			"state" : state
		});
	}
	var url = webRoot + "/" + moduleName + "/" + method;
	if (tips != null) {
		layer.confirm("确认" + tips + "？", {
			icon : 3,
			title : tips
		}, function(index) {
			layer.close(index);
			if (div) {
				$(div).modal("hide");
			}
			wantSth(url, para, function(out) {
				layer.close(layerIndex);
				layer.msg(tips + "成功！", {
					icon : 6
				});
				if (out && out.row) {
					refreshTable(moduleName, out);
				} else {
					refreshTable(moduleName);
				}
			});
		})
	} else {
		wantSth(url, para, function(out) {
			layer.close(layerIndex);
			if (out && out.row) {
				refreshTable(moduleName, out);
			} else {
				refreshTable(moduleName);
			}
		});
	}
}

/**
 * post封装方法，增加了异常处理
 * 
 * @param url
 *            请求路径
 * @param para
 *            参数
 */
function wantSth(url, para, afterSucc) {
	$.post(url, para, function(out) {
		if (typeof (out) == 'string') {
			if (out.indexOf("<title>登录</title>") != -1) {
				location.href = webRoot + "/background/loginWeb";
			} else {
				document.write(out);
			}
			return;
		}
		if (out && out.code == 500) {
			layer.msg(out.msg, {
				icon : 5
			});
			return;
		}
		if (typeof (afterSucc) == 'function') {
			afterSucc(out);
		}
	}).error(function() {
		layer.msg("请求出错！", {
			icon : 7
		});
	});
}

function back() {
	history.back();
}

function clearVal(div) {
	$(div).val("");
}